﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate.Criterion;
using ZDSoft.QLMS.Entity.CreateRule;
using ZDSoft.QLMS.Web.Helpers;
using ZDSoft.QLMS.Entity.SystemSetting;
using ZDSoft.QLMS.Entity;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class SetQuestionTaskController : ControllerBase
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Update()
        {
            return View();
        }

        public string GetTaskByID(int id)
        {
            SetQuestionTask entity = new SetQuestionTask().FindByID(id);
            string jsonStr = "{\"ID\":\"" + entity.ID + "\",\"StartTime\":\"" + entity.StartTime.ToShortDateString().Replace('/', '-') + "\",\"StopTime\":\"" + entity.StopTime.ToShortDateString().Replace('/', '-') + "\",\"IsActive\":\"" + entity.IsActive + "\",\"Remark\":\"" + entity.Remark + "\",\"PublishManID\":\"" + entity.PublishMan.ID + "\",\"CourseName\":\"" + entity.Course.Name + "\"}";
            return jsonStr;
        }

        public string GetCourseByOrg()
        {
            //IList<ICriterion> queryConditions = new List<ICriterion>();
            //queryConditions.Add(Expression.Eq("Organization", AppHelper.LoginedUser.Organization));
            //IList<Course> list = new Course().Find(queryConditions);
            //string jsonStr = "[";
            //foreach (Course entity in list)
            //{
            //    jsonStr += "{\"Name\":\""+entity.Name+"\"}";
            //}
            //jsonStr += "]";
            string jsonStr = "[{\"ID\":\"1\",\"Name\":\"1\"},{\"ID\":\"2\",\"Name\":\"2\"},{\"ID\":\"3\",\"Name\":\"3\"}]";
            return jsonStr;
        }

        [HttpPost]
        public void Create(int courseID,string remark, bool isActive, string startTime,string stopTime)
        {
            SetQuestionTask entity = new SetQuestionTask();
            entity.Course = new Course().FindByID(courseID);
            entity.Remark = remark;
            entity.IsActive = isActive;
            entity.StartTime = DateTime.Parse(startTime);
            entity.StopTime = DateTime.Parse(stopTime);
            entity.PublishMan = AppHelper.LoginedUser;
            entity.Create();
        }

        [HttpPost]
        public void Withdraw(int id)
        {
            SetQuestionTask entity = new SetQuestionTask().FindByID(id);
            if (entity.IsActive == true)
            {
                entity.IsActive = false;
            }
            else
            {
                entity.IsActive = true;
            }
            entity.Update();
        }

        public string GetPaged(int pageIndex, int pageSize, string strSearchProperty,
            string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<SetQuestionTask> tasks = new SetQuestionTask().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);
            string jsonStr = "[";

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (SetQuestionTask entity in tasks)
            {
                jsonStr += "{\"ID\":" + pageNum + ",\"Course\":\"" + entity.Course.Name + "\",\"StartTime\":\"" + entity.StartTime.ToShortDateString() + "\",\"StopTime\":\"" + entity.StopTime.ToShortDateString() + "\",\"PublishMan\":\"" + entity.PublishMan.Account + "\",\"IsActive\":\"" + entity.IsActive + "\"},";
            }
            jsonStr += "{\"ID\":" + pageNum + "}";
            jsonStr += "]";
            return jsonStr;
        }

        public string GetStrategyByUser(int pageIndex, int pageSize, string strSearchProperty,
            string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<SetQuestionTask> tasks = new SetQuestionTask().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);
            IList<UserCourse> ucList = AppHelper.LoginedUser.UserCourses;
            //IList<Course> courseList = new List<Course>();
            IList<SetQuestionTask> userTask = new List<SetQuestionTask>();
            //foreach (UserCourse uc in ucList)
            //{
            //    courseList.Add(uc.Course);
            //}
            foreach (SetQuestionTask task in tasks)
            {
                foreach (UserCourse uc in ucList)
                {
                    if (task.Course.ID == uc.Course.ID)
                    {
                        if (task.StartTime<=DateTime.Now&&DateTime.Now <= task.StopTime)
                        {
                            userTask.Add(task);
                        }
                        break;
                    }
                }
                //if (courseList.Contains(task.Course))
                //{
                //    userTask.Add(task);
                //}
            }
            string jsonStr = "[";

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (SetQuestionTask entity in userTask)
            {
                jsonStr += "{\"ID\":" + pageNum + ",\"Course\":\"" + entity.Course.Name + "\",\"StartTime\":\"" + entity.StartTime.ToShortDateString() + "\",\"StopTime\":\"" + entity.StopTime.ToShortDateString() + "\",\"PublishMan\":\"" + entity.PublishMan.Account + "\",\"IsActive\":\"" + entity.IsActive + "\"},";
            }
            jsonStr += "{\"ID\":" + pageNum + "}";
            jsonStr += "]";
            return jsonStr;
        }
    }
}
